home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / doc / gpc / demos / filelockdemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-08  |  4.2 KB  |  107 lines

  1. {
  2. GPC demo program for the FileLock and FileUnlock routines.
  3.  
  4. Copyright (C) 1999-2001 Free Software Foundation, Inc.
  5.  
  6. Author: Frank Heckenbach <frank@pascal.gnu.de>
  7.  
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, version 2.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; see the file COPYING. If not, write to
  19. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.
  21.  
  22. As a special exception, if you incorporate even large parts of the
  23. code of this demo program into another program with substantially
  24. different functionality, this does not cause the other program to
  25. be covered by the GNU General Public License. This exception does
  26. not however invalidate any other reasons why it might be covered
  27. by the GNU General Public License.
  28. }
  29.  
  30. program FileLockDemo;
  31.  
  32. uses GPC;
  33.  
  34. var
  35.   f : File;
  36.   Res : Boolean;
  37.   TempFileName : TString;
  38.  
  39. { The program calls itself recursively, so the instances can try
  40.   file locks against each other }
  41. procedure DoExecute (const Msg, Parameter : String);
  42. var Dummy : Integer;
  43. begin
  44.   Write (Msg);
  45.   Flush (Output);
  46.   Dummy := Execute (ParamStr (0) + ' --recursive ' + Parameter + ' ' + TempFileName)
  47. end;
  48.  
  49. procedure TimerHandler (Signal : Integer);
  50. begin
  51.   Writeln ('Received signal `', StrSignal (Signal), '''.')
  52. end;
  53.  
  54. begin
  55.   { Recursive call }
  56.   if ParamStr (1) = '--recursive' then
  57.     begin
  58.       Reset (f, ParamStr (3), 1);
  59.       if ParamStr (2) = '--read' then
  60.         Writeln (FileLock ((*@@*)AnyFile( f), False, False))
  61.       else if ParamStr (2) = '--write' then
  62.         Writeln (FileLock ((*@@*)AnyFile( f), True, False))
  63.       else if ParamStr (2) = '--block' then
  64.         begin
  65.           Writeln ('Subprocess write lock, should fail:                  ', FileLock ((*@@*)AnyFile( f), True, False));
  66.           Writeln ('Setting an alarm to 5 seconds, should succeed:       ', Alarm (5) >= 0);
  67.           Writeln ('Install timer handler, should succeed:               ', InstallSignalHandler (SigAlrm, TimerHandler, False, False, null, null));
  68.           Writeln ('A write lock with blocking, should block the program.');
  69.           Writeln ('However, in 5 seconds, the program should get an `Alarm clock'' signal...');
  70.           Res := FileLock ((*@@*)AnyFile( f), True, True);
  71.           Writeln ('The write lock should have failed,');
  72.           Writeln ('because it was interrupted:                          ', Res)
  73.         end
  74.       else
  75.         Writeln (ParamStr (0), ': invalid recursive call.');
  76.       Close (f);
  77.       Halt
  78.     end;
  79.  
  80.   { Non-recursive call. Execution normally starts here. }
  81.  
  82.   TempFileName := GetTempFileName;
  83.   Rewrite (f, TempFileName, 1);
  84.  
  85.   Writeln   ('`True'' means success, `False'' means failure.');
  86.  
  87.   DoExecute ('Subprocess read lock, should succeed:                ', '--read');
  88.   DoExecute ('Subprocess write lock, should succeed:               ', '--write');
  89.  
  90.   Writeln   ('Read lock, should succeed:                           ', FileLock ((*@@*)AnyFile( f), False, False));
  91.   DoExecute ('Subprocess read lock, should succeed:                ', '--read');
  92.   DoExecute ('Subprocess write lock, should fail:                  ', '--write');
  93.   Writeln   ('Remove lock, should succeed:                         ', FileUnLock ((*@@*)AnyFile( f)));
  94.  
  95.   Writeln   ('Write lock, should succeed:                          ', FileLock ((*@@*)AnyFile( f), True, False));
  96.   DoExecute ('Subprocess read lock, should fail:                   ', '--read');
  97.   DoExecute ('Subprocess write lock, should fail:                  ', '--write');
  98.   DoExecute ('Subprocess write lock with blocking...' + NewLine     , '--block');
  99.   Writeln   ('Remove lock, should succeed:                         ', FileUnLock ((*@@*)AnyFile( f)));
  100.  
  101.   DoExecute ('Subprocess read lock, should succeed:                ', '--read');
  102.   DoExecute ('Subprocess write lock, should succeed:               ', '--write');
  103.  
  104.   Close (f);
  105.   Erase (f)
  106. end.
  107.